package com.mocaris.android.gank.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.mocaris.android.gank.R
import com.mocaris.android.gank.model.Banner
import kotlinx.android.synthetic.main.item_banner.view.*

/**
 * banner adapter
 */
class BannerAdapter(private var context: Context) : PagerAdapter() {

    var bannerList = listOf<Banner>()
        set(value) {
            field = value
            notifyDataSetChanged()
        }


    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return bannerList?.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(context).inflate(R.layout.item_banner, container, false)
        val banner = bannerList[position]
        view.tv_desc.text = banner.title
        Glide.with(view).load(banner.imagePath).into(view.img_banner)
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View?)
    }
}